package wf.source.hrm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import wf.source.hrm.domain.Tenant;
import wf.source.hrm.query.TenantQuery;
import wf.source.hrm.service.ITenantService;
import wf.source.hrm.util.AjaxResult;
import wf.source.hrm.util.PageList;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tenant")
public class TenantController {
    @Autowired
    public ITenantService tenantService;

    /**
    * 保存和修改公用的
    * @param tenant  传递的实体
    * @return Ajaxresult转换结果
    */
    @PutMapping
    public AjaxResult addOrUpate(@RequestBody Tenant tenant){
        if(tenant.getAdmin().getEmail()!=null){

            String password=tenant.getAdmin().getPassword();
            String confirmPwd=tenant.getAdmin().getComfirmPassword();

            if(!StringUtils.hasLength(password)||!StringUtils.hasLength(confirmPwd)){
                return AjaxResult.me().setSuccess(false).setMessage("请输入密码或确认密码");
            }
            if(!password.equals(confirmPwd)){
                return AjaxResult.me().setSuccess(false).setMessage("两次输入的密码不一致，请确认");
            }
        }
        try {
            if(tenant.getId()!= null)
                tenantService.updateById(tenant);
            else
			    tenantService.insert(tenant);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            tenantService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }


	
    //获取用户
    @GetMapping("/{id}")
    public Tenant get(@PathVariable("id")Long id)
    {
        return tenantService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Tenant> list(){

        return tenantService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Tenant> json(@RequestBody TenantQuery query){
        return tenantService.selectPageList(query);
    }

    /**
     * 机构的首字母
     * @return
     */
    @GetMapping("/letters")
    public Map<String,Object> getLetters(){
        return tenantService.getLetters();
    }
}
